package com.jiankangjin.weixinchannel.util;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.Map.Entry;
import java.util.TreeMap;

import com.jiankangjin.weixinchannel.config.WeixinConfig;

public class MD5Util {

	private static String byteArrayToHexString(byte b[]) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++)
			resultSb.append(byteToHexString(b[i]));

		return resultSb.toString();
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n += 256;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	/**
	 * 传入Map输出已MD5签名好的Map
	 */
	public static Map<String, Object> MD5Encode(Map<String, Object> map, String charsetname) {
		SortedMap<String, String> paramsMap = new TreeMap<String, String>();
		for (String key : map.keySet()) {
			if (!"sign".equals(key) && null != map.get(key) && !"".equals(map.get(key))) {
				paramsMap.put(key, map.get(key).toString());
			}
		}
		StringBuffer sb = new StringBuffer();
		Iterator<Entry<String, String>> it = paramsMap.entrySet().iterator();
		while (it.hasNext()) {
			Entry<String, String> entry = (Entry<String, String>) it.next();
			sb.append(entry.getKey() + "=" + entry.getValue() + "&");
		}
		sb.append("key=" + WeixinConfig.PARTNER_KEY);
		//System.out.println("MD5签名前的字符串:"+sb);
		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.putAll(paramsMap);
		returnMap.put("sign", MD5Encode(sb.toString(), charsetname).toUpperCase());
		return returnMap;
	}
	
	public static String MD5Encode(Map<String, Object> map) {
		SortedMap<String, String> paramsMap = new TreeMap<String, String>();
		for (String key : map.keySet()) {
			if (!"sign".equals(key) && null != map.get(key) && !"".equals(map.get(key))) {
				paramsMap.put(key, map.get(key).toString());
			}
		}
		StringBuffer sb = new StringBuffer();
		Iterator<Entry<String, String>> it = paramsMap.entrySet().iterator();
		while (it.hasNext()) {
			Entry<String, String> entry = (Entry<String, String>) it.next();
			sb.append(entry.getKey() + "=" + entry.getValue() + "&");
		}
		sb.append("key=" + WeixinConfig.PARTNER_KEY);
		return MD5Encode(sb.toString(), "utf8").toUpperCase();
	}
	
	public static String MD5Encode(String origin, String charsetname) {
		String resultString = null;
		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			if (charsetname == null || "".equals(charsetname))
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes()));
			else
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes(charsetname)));
		} catch (Exception exception) {
		}
		return resultString;
	}

	private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

}
